package com.yl.applicationsecurity.controller;

import com.github.pagehelper.PageInfo;
import com.yl.applicationsecurity.pojo.*;
import com.yl.applicationsecurity.service.PathMonitoringService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 鹤仙问鹿仙
 * @program: Application-security
 * @description: 接口监控功能控制类
 * @date 2025-04-23 18:08:35
 */
@RestController
@RequestMapping("/pathMonitoring")
public class PathMonitoringController {


    @Autowired
    private PathMonitoringService pathMonitoringService;

    @PostMapping("/list")
    public Result list(String[] nationSelect, PathMonitoring pathMonitoring, @RequestParam(required = false) Date[] startAndEndDate, @RequestParam(defaultValue = "1", value = "pageNum")Integer pageNum
            , @RequestParam(defaultValue = "10", value = "pageSize")Integer pageSize ,Integer permissionCheck, Integer unauthorizedCheck){

        PageInfo<PathMonitoring> pathMonitoringPageInfo = null;

        if(startAndEndDate!=null&&startAndEndDate.length!=2){
            pathMonitoringPageInfo =  pathMonitoringService.findListByPathMonitoring(nationSelect,pathMonitoring,null,null,pageNum,pageSize,permissionCheck,unauthorizedCheck);
        }else{
            pathMonitoringPageInfo =  pathMonitoringService.findListByPathMonitoring(nationSelect,pathMonitoring,startAndEndDate[0],startAndEndDate[1],pageNum,pageSize,permissionCheck,unauthorizedCheck);
        }

        return Result.success(pathMonitoringPageInfo);
    }


    /**
     * 提供给安全雷达的接口，用来传输接口监控源数据
     * @param list
     * @return
     */
    @RequestMapping("/insertPathMonitoring")
    public Result insertPathMonitoring(@RequestBody List<PathMonitoring> list) {
        for (PathMonitoring pathMonitoring : list) {
            pathMonitoringService.insertPathMonitoring(pathMonitoring);
        }
        return  Result.success("添加成功");
    }


    @RequestMapping("/insertCheckStatus")
    public Result insertCheckStatus(@RequestBody List<PathMonitoringInsert> list) {
        PathMonitoring pathMonitoring = null;
        for (PathMonitoringInsert pathMonitoringInsert : list) {
            pathMonitoring = new PathMonitoring();
            pathMonitoring.setUri(pathMonitoringInsert.getUri());
            pathMonitoring.setAppName(pathMonitoringInsert.getAppName());
            pathMonitoring.setNation(pathMonitoringInsert.getNation());
            pathMonitoring.setFullPath(pathMonitoringInsert.getUri());
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            try {
                if(pathMonitoringInsert.getPermissionCheckDateStr()!=null){
                    Date permissionCheckDate = formatter.parse(pathMonitoringInsert.getPermissionCheckDateStr());
                    pathMonitoring.setPermissionCheckDate(permissionCheckDate);
                }

            } catch (ParseException e) {
                e.printStackTrace();
            }
            try {
                if(pathMonitoringInsert.getUnauthorizedCheckDateStr()!=null){
                    Date unauthorizedCheckDate = formatter.parse(pathMonitoringInsert.getUnauthorizedCheckDateStr());
                    pathMonitoring.setUnauthorizedCheckDate(unauthorizedCheckDate);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }



            pathMonitoringService.insertCheckStatus(pathMonitoring);
        }
        return  Result.success("添加成功");
    }



}

